/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasi?                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ui_uiexporttorrent.h"
#include "exporttorrentdialog.h"

#include "globals.h"
#include "trackerdb.h"
#include "bencoding.h"

#include "modeluser.h"
#include "modeltorrent.h"

#include <QtCore>
#include <QtGui>

ExportTorrentDialog::ExportTorrentDialog(const QString& torrentName, QWidget* parent, Qt::WFlags fl)
		: QDialog(parent, fl), Ui::Dialog(), m_torrentName(torrentName)
{
	setupUi(this);
	fillUsersCombo();

	connect(btnButtons, SIGNAL(accepted()), this, SLOT(accept()));
}

ExportTorrentDialog::~ExportTorrentDialog()
{}

/*$SPECIALIZATION$*/
void ExportTorrentDialog::accept()
{
	exportTorrentForUser(cmbUsers->currentText());

	QDialog::accept();
}

void ExportTorrentDialog::fillUsersCombo()
{
	//QComboBox* cmbUsers;
	QStringList users = Globals::instance().db()->getAllUsernames();
	cmbUsers->clear();
	cmbUsers->addItems(users);
}

void ExportTorrentDialog::exportTorrentForUser(const QString& username)
{
	ModelUser* user = Globals::instance().db()->createModelUser(username);
	ModelTorrent* torrent = Globals::instance().db()->createModelTorrent(m_torrentName);

	ModelUser* uploadedBy = Globals::instance().db()->createModelUser(torrent->uploadedBy());

	//Get original metainfo data
	QByteArray metainfo = torrent->metainfo();

	QByteArrayMatcher matcher("d8:announce");

	int announceDictStart = matcher.indexIn(metainfo);

	int announceUrlStart = announceDictStart + 11;
	//opasan kod ovde!!!
	while (metainfo.at(announceUrlStart++) != ':') {}

	QString urlLen = metainfo.mid(announceDictStart + 11, announceUrlStart - 12);
	QString url = metainfo.mid(announceUrlStart, urlLen.toInt());
	QString newurl = url;

	newurl.replace(uploadedBy->passkey(), user->passkey());
	metainfo.replace(url.toAscii(), newurl.toAscii());

	QString fn = QFileDialog::getSaveFileName(this, tr("Choose file"), QString(), tr("Torrents (*.torrent)"));

	if (!fn.isEmpty())
	{
		QFile file(fn);
		file.open(QIODevice::WriteOnly);
		file.write(metainfo);
		file.close();
	}

	//Dispose all objects
	delete uploadedBy;
	delete user;
	delete torrent;
}

